-- 特制keyBoard 类
-- 不要复制到别的地方使用
KeyBoard = class()

function KeyBoard:init(keyPressed,keyboardShowed,keyboardHidden)
    self.buffer = {}
    self:setFunction(keyPressed,keyboardShowed,keyboardHidden)
    keyboard = function(key) self:keyboard(key) end
end

function KeyBoard:setFunction(keyPressed,keyboardShowed,keyboardHidden)
    self.keyPressed = keyPressed
    self.keyboardShowed = keyboardShowed
    self.keyboardHidden = keyboardHidden
end

function KeyBoard:keyboard(key)
    if key == string.char(10) then
        key = nil
        self:hide()
    end
    
    if key == BACKSPACE then
        table.remove(self.buffer)
    else
        table.insert(self.buffer,key)
    end
    if self.keyPressed then
        self.keyPressed(key)
    end
end

function KeyBoard:getBuffer()
    return table.concat(self.buffer)
end

function KeyBoard:clearBuffer()
    self.buffer = {}
end

function KeyBoard:show()
    showKeyboard()
    if self.keyboardShowed then
        self.keyboardShowed()
    end
end

function KeyBoard:hide()
    hideKeyboard()
    if self.keyboardHidden then
        self.keyboardHidden()
    end
end

function KeyBoard:isShowing()
    return isKeyboardShowing()
end

